<?php
 /**************************************************
* PluginLotto.com                                 *
* Copyrights (c) 2005-2010. iZAP                  *
* All rights reserved                             *
***************************************************
* @author iZAP Team "<support@izap.in>"
* @link http://www.izap.in/
* @version {version} $Revision: {revision}
* Under this agreement, No one has rights to sell this script further.
* For more information. Contact "Tarun Jangra<tarun@izap.in>"
* For discussion about corresponding plugins, visit http://www.pluginlotto.com/pg/forums/
* Follow us on http://facebook.com/PluginLotto and http://twitter.com/PluginLotto
 */

class izapPoll extends IzapObject {
    function __construct($guid = 0, $params = array()) {
    parent::__construct($guid, $params);
  }

  protected function initialise_attributes() {
    parent::initialise_attributes();
  }

 public function get_attributes_array() {
     $array  = array(
      'title' => array('required' => TRUE, 'type' => 'string'),
      'access_id' => array('required' => TRUE, 'type' => 'numerical', 'default' => ACCESS_PUBLIC ),
      //'option' => array('required' => TRUE, 'type' => 'string'),
      'description' => array('required' => FALSE, 'type' => 'string'),
      'tags' => array('required' => FALSE, 'type' => 'tags'),
    );

     if($this->guid) { // if in case of edit
        // $array['option'] = array('required' => false, 'type' => 'string');
     }
     return $array;
  }

  /*
   * function to save entity
   */

  public function save() {
      if(parent::save()) {
          return TRUE;
      }
      return FALSE;
  }

  /*
   *  function for getting URL of entity
   */

  public function getUrl() {
      global $CONFIG;
      $url = func_set_href_byizap( array(
        'plugin' => GLOBAL_IZAP_POLL_PLUGIN,
          'page' => 'view',
          'page_owner'=>$this->getOwnerEntity()->username,
        'vars'=> array( $this->guid, friendly_title($this->title) )));
     return $url;
  }

  public function getIcon($size = 'medium') {
      return $this->getOwnerEntity()->getIcon($size);
  }

}

function izap_calculate_percentage($value, $total_value, $round = 2) {
  if(!$value) {
    return 0;
  }
  $precent = ($value/$total_value) * 100;

  return round($precent, $round);
}

function get_random_poll_entities() {
    $all_polls = elgg_get_entities(array(
      'type' => 'object',
      'subtype' => GLOBAL_IZAP_POLL_SUBTYPE,
      'limit' => 999 ));
    $i = rand(0,(count($all_polls) - 1));
    return $all_polls[$i];
}